<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
import { debounce } from '@/utils'

export default {
props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '450px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object
    }
  },
  data() {
    return {
      chart: null,
      selfData: this.chartData
    }
  },
  mounted() {
    this.initChart()
    let _this = this;
    if (this.autoResize) {
      this.__resizeHanlder = debounce(() => {
        if (_this.chart) {
          _this.chart.resize()
        }
      }, 100)      
      window.addEventListener('resize', this.__resizeHanlder)
    }
    // 监听侧边栏的变化
    // const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    // sidebarElm.addEventListener('transitionend', this.__resizeHanlder)
  },
  beforeDestroy() {
    if (!this.chart) {
     return
    }
    if (this.autoResize) {
     window.removeEventListener('resize', this.__resizeHanlder)
    }
    // const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    // sidebarElm.removeEventListener('transitionend', this.__resizeHanlder)
    this.chart.dispose()
    this.chart = null
  },
  created() {
  },
  watch: {
    chartData: {
      deep: true,
      // immediate: true,
      // setTimeout(() => {
        handler(val) {
      //   // let m  = this.setSeries()
      //   console.log('组件的值改变了')
      //   console.log(val)
        this.setOptions(val)
      }
      // }, 10)
    },
    "chartData.xAxisData": function(newVal,oldVal) {
      // this.setOptions(this.chartData)
      // console.log('新值')
      // console.log(newVal)
    }
  },
  methods: {
    
    setOptions(chartData) {
      
      let _this = this, series = this.setSeries(chartData)
     
      // setInterval(function() {
      //   console.log(_this.chartData)
      // }, 100);
      this.chart.setOption({
        xAxis: {
          data: chartData.xAxisData,
          boundaryGap: false,
          axisLabel: {
            rotate:40, 
          },
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',  
          padding: [5, 10]
        },
        yAxis: [
          {
            name: '',
            type: 'value',
        },
        { 
            type: 'value',
            max:100,
            min:0,
            axisLabel: {  
                  // show: true,  
                  interval: 'auto',  
                  formatter: '{value} %'  
                }, 
            show: true  
        }],
        legend: {
          data: chartData.legendData
        },
        series: series 
        })
    },
    setSeries(chartData) {
      let series = [], _this = this;
      let itemStyle = {  
          normal: {  
              label: {  
                  show: true,  
                  position: 'top',  
                  formatter: '{b}\n{c}%'  
              }  
          }  
      }
      chartData.legendData.forEach(function(v, i) {
        let obj = {
          type: 'line',
          smooth: true,
          name: '',
          data: []
        };
        if(v === '成功率') {
          obj['yAxisIndex'] = 1
        }
        obj.name = v
        obj.data = chartData.seriesData[i]
        series.push(obj) 
      })
     return series
    },
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    }
  }
}
</script>

<style>
    .chart{
      min-width:500px;
    }
</style>
